package com.rzico.basics.amap.util;

import com.alibaba.fastjson.JSONObject;
import com.rzico.basics.amap.pojo.district.DistrictList;
import com.rzico.basics.amap.pojo.request.DistrictRequest;

/**
 * 行政区域查询
 * <p>
 * Created by lam on 2018/8/8.
 */
public class GeoDistrictUtil {

    // 行政区域查询API服务地址
    private static String map_district_url = "https://restapi.amap.com/v3/config/district?parameters";

    /**
     * 行政区域查询
     *
     * @param request
     * @return
     */
    public static DistrictList searchGeoDistrict(DistrictRequest request) {
        DistrictList list = null;

        if (request == null) {
            list = new DistrictList();
            list.setError(true);
            list.setMessage("请输入需要查询的区域参数");
            return list;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            list = new DistrictList();
            list.setError(true);
            list.setMessage("请输入请求服务权限的key");
            return list;
        }

        try {
            String result = OKHttpUtil.httpGet(map_district_url.replace("parameters", "") + request.toString());
            list = JSONObject.parseObject(result, DistrictList.class);

            if (list != null && list.getStatus().equals("0")) {
                list.setError(true);
                list.setMessage(list.getInfo());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
